<?php

require_once 'model/ModelEvent.php';
require_once 'model/ModelUser.php';
require_once 'model/ModelPhoto.php';
require_once 'model/ModelComment.php';

class ControllerBackOffice {

    function index() {
        if (isset($_SESSION['connected']) && $_SESSION['connected'] == "1") {
            $content['title'] = "Accueil back office";
            $content['nom_menu'] = "Back Office";
            $content['content1'] = "Bienvenue sur la partie qui permet d'administrer
                                votre site Internet";
            include 'view/backOffice/index.php';
        } else {
            $content['title'] = "Identification";
            $content['nom_menu'] = "Identification";
            include 'view/backOffice/login.php';
        }
    }

    function event() {
        if (isset($_SESSION['connected']) && $_SESSION['connected'] == "1") {
            //l'utilisateur à modifié l'évènement
            if (!empty($_POST['id']) && !empty($_POST['title']) && !empty($_POST['content'])
                    && !empty($_POST['day']) && !empty($_POST['month'])
                    && !empty($_POST['year']) && !empty($_POST['minutes'])
                    && !empty($_POST['hour'])
            ) {
                $model = new ModelEvent();
                $article = $model->exist($_POST['id']);
                if ($article != "empty") {
                    $date = $_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'];
                    $hour = $_POST['hour'] . ':' . $_POST['minutes'];
                    echo $date;
                    $id = $_POST['id'];
                    $title = $_POST['title'];
                    $contentT = addslashes($_POST['content']);
                    $model->update($id, $title, $contentT, $date, $hour);
                }
            }
            //Si l'utilisateur veut supprimer l'évènement
            if (!empty($_POST['delete'])) {
                $model = new ModelEvent();
                $article = $model->exist($_POST['id']);
                if ($article != "empty") {
                    $model->delete($_POST['id']);
                }
            }
            $content['title'] = "Accueil back office";
            $content['nom_menu'] = "Back Office";
            $content['content1'] = "";
            $model = new ModelEvent();
            $content['event'] = $model->selectAll();
            include 'view/backOffice/event.php';
        } else {
            $this->index();
        }
    }

    function identification() {
        if (!empty($_POST)) {
            if (!empty($_POST['user']) && !empty($_POST['passwd'])) {
                $model = new ModelUser();
                $user = $_POST['user'];
                $passwd = md5($_POST['passwd']);
                if ($model->connect($user, $passwd)) {
                    $_SESSION['connected'] = "1";
                }
            }
        }
        $this->index();
    }

    function editEvent() {
        //L'utilisateur a cliqué sur ajouter évènement        
        $model = new ModelEvent();
        $modelComment = new ModelComment();
        if (!empty($_POST['add'])) {
            $_POST['id'] = $model->insert();
        }
        if (!empty($_POST['imgUpload'])) {
            require_once 'controller/ControllerImgUpload.php';
            $imgUpload = new ControllerImgUpload();
            $imgUpload->setDirectory("view/images/");
            if ($imgUpload->checkExtension()) {
                $imgUpload->upload("imgFile");
                $model->addImg($_POST['id'], $imgUpload->getPath());
            } else {
                //Mauvaise extension
            }
        }
        if (!empty($_POST['deleteComment'])) {
            $modelComment->delete($_POST['comment_id']);
        }
        $content['comment'] = $modelComment->selectAll($_POST['id']);
        if (!empty($_POST['id'])) {

            $article = $model->exist($_POST['id']);
            if ($article != "empty") {
                while ($row = mysql_fetch_array($article)) {
                    $content['eventId'] = $row['EVENT_ID'];
                    $content['eventTitre'] = $row['EVENT_TITLE'];
                    $content['eventContent'] = $row['EVENT_CONTENT'];
                    $content['eventImg'] = $row['EVENT_IMG'];
                    $content['year'] = substr($row['EVENT_TIME'], 0, 4);
                    $content['month'] = substr($row['EVENT_TIME'], 5, 2);
                    $content['day'] = substr($row['EVENT_TIME'], 8, 2);
                    $content['hour'] = substr($row['EVENT_HOUR'], 0, 2);
                    $content['minutes'] = substr($row['EVENT_HOUR'], 3, 2);
                }
                $content['title'] = "Edition Evènement";
                $content['nom_menu'] = "évènement";
                include 'view/backOffice/editEvent.php';
                return;
            }
        }
        $content['title'] = "Erreur";
        $content['nom_menu'] = "Erreur";
        $content['content1'] = "Oups, l'évènenement sélectionné n'existe pas<br>Merci de recommencer";
        include 'view/backOffice/error.php';
    }

    function photos() {
        $model = new ModelPhoto();
        if (isset($_SESSION['connected']) && $_SESSION['connected'] == "1") {
            if (!empty($_POST['imgUpload'])) {
                require_once 'controller/ControllerImgUpload.php';
                $imgUpload = new ControllerImgUpload();
                $imgUpload->setDirectory("view/photos/");
                if ($imgUpload->checkExtension()) {
                    $imgUpload->upload("imgFile");
                    $model->insert($imgUpload->getPath());
                } else {
                    //Mauvaise extension
                }
            }
            if (!empty($_POST['photo_to_delete'])) {
                for ($i = 0, $c = count($_POST['photo_to_delete']); $i < $c; $i++) {
                    $model->delete($_POST['photo_to_delete'][$i]);
                }

                $content['title'] = "Edition photos";
                $content['nom_menu'] = "Photo";
                include 'view/backOffice/photo.php';
                return;
            }
            $content['title'] = "Edition photos";
            $content['nom_menu'] = "Photos";
            $content['photos'] = $model->getPhotos();
            include 'view/backOffice/photo.php';
        } else {
            $content['title'] = "Erreur";
            $content['nom_menu'] = "Erreur";
            $content['content1'] = "Oups, l'évènenement sélectionné n'existe pas<br>Merci de recommencer";
            include 'view/backOffice/error.php';
        }
    }

}

?>
